package com.hs.telepass.controller.task;


import android.content.Intent;
import com.hs.telepass.controller.TaskController;
import com.hs.telepass.model.json.response.BaseResponseJson;
import com.hs.telepass.utils.JsonUtil;
import com.hs.telepass.utils.StringUtil;

public class SendAuthTask extends Task{
    public final static String ACTION_SEND_AUTH_TASK = "action_send_auth_task";

    private String jsonRequstStr;
    private String taskUrl;
    private int requestType;

    public SendAuthTask(String sid, String req, String encrypted_payload){
        jsonRequstStr = "sid=" + sid + "&" + "req=" + req + "&" + "encrypted_payload=" + encrypted_payload;
        requestType = TASK_REQUST_TYPE_POST;
        taskUrl = BASE_API_URL + "/app/" + "auth";
    }

    @Override
    public void onExecuted(String response, int httpCode) {
        Intent intent = new Intent(getActionName());

        BaseResponseJson json = JsonUtil.fromJson(response, BaseResponseJson.class);

        String code = json.getCode();
        if("200".equals(code)){
            intent.putExtra(TaskController.EXTRA_EVENT, TaskController.EVENT_TASK_SUCCEED);
        }else {
            intent.putExtra(TaskController.EXTRA_EVENT, TaskController.EVENT_TASK_ERROR);
        }

        broadcastManager.sendBroadcast(intent);
    }

    @Override
    public int getRequstType() {
        return requestType;
    }

    @Override
    public String getJsonRequstStr() {
        return jsonRequstStr;
    }

    @Override
    public int getResponseSuccessCode() {
        return 0;
    }

    @Override
    public String getTaskUrl() {
        return taskUrl;
    }

    @Override
    public String getActionName() {
        return ACTION_SEND_AUTH_TASK;
    }
}
